package com.videoweb.controller;

import com.videoweb.entity.Menu;
import com.videoweb.service.IMenuService;
import com.videoweb.utils.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lmr
 * @since 2025-05-17
 */
@RestController
@RequestMapping("/system/menu")
public class MenuController {

    @Autowired
    private IMenuService menuService;

    // 获取菜单树结构
    @GetMapping("/treeselect")
    public AjaxResult treeselect() {
        List<Menu> menus = menuService.selectMenuTreeAll();
        return AjaxResult.success(menuService.buildMenuTreeSelect(menus));
    }

    // 根据角色ID获取菜单树结构（已分配菜单）
    @GetMapping("/roleMenuTreeselect/{roleId}")
    public AjaxResult roleMenuTreeselect(@PathVariable("roleId") Long roleId) {
        List<Menu> menus = menuService.selectMenuTreeAll();
        List<Long> checkedKeys = menuService.selectMenuIdsByRoleId(roleId);
        AjaxResult ajax = AjaxResult.success();
        ajax.put("menus", menuService.buildMenuTreeSelect(menus));
        ajax.put("checkedKeys", checkedKeys);
        return ajax;
    }
}
